
#include "omip.h"
//#include "zone.h"

#define FLASH_SECTOR_SIZE		0x1000 
#define FLASH_READ_AND_WRITE_MAX_LEN 65535
#define FLASH_RW_MAX_LEN 0xFFFF
#define FILE_BIN_FLAG_ADDR 0X200400
#define FILE_BIN_LENGTH_ADDR 0X200402
#define FILE_BIN_UPDATE_FLAG_ADDR 0X200406


#define FILE_BL_SN_ADDR 	0x000000	//FLASH中SN存放地址
#define FILE_BL_SN_SIZE 	64			//FLASH中SN存放长度

#define FILE_BL_DEV_ADDR 	0x000041	//FLASH中设备信息存放地址
#define FILE_BL_DEV_SIZE 	512			//FLASH中设备信息存放长度

#define FILE_DEV_STA_ADDR 	0x000241	//FLASH中状态数据存放地址
#define FILE_DEV_STA_SIZE 	64			//FLASH中状态数据存放长度

//第1扇区
#define FILE_FLOW_ADDR 		0x000441	//FLASH中策略文件存放地址
#define FILE_FLOW_SIZE 		1024*1024	//FLASH中策略文件存放长度

//256扇区
#define FILE_BIN_ADDR 		0x100441	//FLASH中MCU固件存放地址
#define FILE_BIN_SIZE 		1024*1024	//FLASH中MCU固件存放长度

#define VO_DP_POINT_ADDR	0x200500	//FLASH中点位信息存放地址
#define VO_DP_POINT_LEN		144 		//16*9  sizeif(priority_data_t)*16 FLASH中点位信息存放长度


#define MOTOR_MAX_COEF_ADDR 	(VO_DP_POINT_ADDR+VO_DP_POINT_LEN)
#define MOTOR_MAX_COEF_LEN 	2
#define MOTOR_MIN_COEF_ADDR 	(MOTOR_MAX_COEF_ADDR+MOTOR_MAX_COEF_LEN)
#define MOTOR_MIN_COEF_LEN 	2

//256扇区
#define FILE_FLOW_FLAG_ADDR 0x100400
#define FILE_FLOW_LENGTH_ADDR 0x100402
#define FILE_FLAG_LENGTH 2
#define FILE_LENGTH_LENGTH 4

/*日志存放区域 4080~4087扇区，32K*/
#define FILE_MCU_LOG_SECTOR_NO 4080
#define FILE_MCU_LOG_ADDR (FILE_MCU_LOG_SECTOR_NO * FLASH_SECTOR_SIZE)
#define FILE_MCU_LOG_SIZE            0x00008000  //32k


#define FILE_BL_BKSN_ADDR 	4096*4095	//FLASH中备份SN存放地址 最后一个扇区
#define FILE_BL_BKSN_SIZE 	64			//FLASH中备份SN存放长度

#define USE_SRAM_BCKUP

typedef enum {
    TEMP_R_MAP_ZONE = 0, //ntc温度电阻映射表
    LOG_RECORD_ZONE,
} flash_zone_id;


typedef struct w25flash 
{
    uint8_t   	file_name[16];
	char *	 	buff;
	uint32_t   	flash_start_addr;
	uint32_t   	size;
}w25flash_t;

#ifdef USE_SRAM_BCKUP
typedef struct sram
{
    uint8_t   file_name[16];
	uint8_t*   buff;
	uint32_t   flash_start_addr;
	uint32_t   size;
}sram_t;

typedef struct flash_zone{
    flash_zone_id id;
	uint32_t secpos_id; 
	uint32_t zone_size; 
    unsigned int start_address;
    unsigned int end_address;
}flash_zone_t;

#endif

int mcu_flash_read(void* buf, int32_t len, uint32_t location);
int mcu_flash_write(void* buf, int32_t len, uint32_t location);
zone_info *mcu_get_flash_zone(zone_id id);
int mcu_erase_sector_flash(zone_id id, unsigned int address, int length);
int mcu_erase_flash(zone_id id, int length);
int mcu_read_flash(zone_id id, unsigned int address, uint8_t* buffer, unsigned int length);
int mcu_write_flash(zone_id id, unsigned int address, const uint8_t* buffer, unsigned int length);


void free_w25flash_t(w25flash_t **pData);
w25flash_t *file_addr_init(char *filename, uint32_t start_addr, uint32_t size);
void read_data_from_file(w25flash_t *file);
int save_data_to_file(w25flash_t *file);

#ifdef USE_SRAM_BCKUP
sram_t *file_addr_init_ext(uint8_t *filename, uint32_t start_addr, uint32_t size);
void read_data_from_file_ext(sram_t *file);
int save_data_to_file_ext(sram_t *file);
int flash_rw_zone_test();
int flash_erase_zone(zone_id id);

#endif
